package main;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import both.map.*;
import both.map.mapModel.*;
import both.map.mapView.*;
import both.character.*;
import client.*;

public class REDAD implements MapListener, ClientListener {
	
	private StandardMapModel mapModel;
	private MapDisplay mapDisplay;
	private MapBuilder mapBuilder;
	private CharacterDisplay charDisplay;
	private JFrame frame;
	private JPanel mapHolder, content;
	private BoxLayout mainLayout;
	private DnDClient client;
	
	public REDAD() {
		frame = new JFrame("Remote D & D"); 
		content = (JPanel) frame.getContentPane();
		
		mapModel = new StandardMapModel();
		
		mapDisplay = new MapDisplay();
		mapDisplay.setEnabled(false);
		mapDisplay.createFirstChunk();
		mapDisplay.addMapListener(this);
				
		mapBuilder = new MapBuilder();
		
		charDisplay = new CharacterDisplay();
		
		mapHolder = buildMapArea();
						
		client = new DnDClient();
		
		buildWindow();
		drawLogo();
	}
	
	public JPanel buildMapArea() {
		JPanel panel = new JPanel();
		BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
		panel.setLayout(layout);	
		
		JScrollPane scrollPane = new JScrollPane(mapDisplay);
				
		panel.add(mapBuilder);
		panel.add(scrollPane);
		
		return panel;
	}
	
	public void buildWindow() {
		mainLayout = new BoxLayout(content, BoxLayout.PAGE_AXIS);
		content.setLayout(mainLayout);
		
		content.add(charDisplay);
		content.add(mapHolder);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public void drawLogo() {
		mapDisplay.updateMapArea(0, 0, 0, 19, 19, TerrainType.grass);
		mapDisplay.updateMapArea(0, 0, 4, 19, 13, TerrainType.dirt);
		mapDisplay.updateMapArea(0, 1, 5, 1, 12, TerrainType.stoneWall);
		mapDisplay.updateMapArea(0, 1, 5, 3, 5, TerrainType.stoneWall);
		mapDisplay.updateMapArea(0, 3, 5, 3, 8, TerrainType.stoneWall);
		mapDisplay.updateMapArea(0, 1, 8, 3, 8, TerrainType.stoneWall);
		//mapDisplay.updateMapArea(0, 0, 0, 19, 19, TerrainType.stoneWall);
	}
	
	@Override
	public void mapEventPerformed(MapEvent e) {
		if (!e.isArea()) {
			mapDisplay.updateMap(0, e.getX(), e.getY(), mapBuilder.getSelected());
		} else {
			mapDisplay.updateMapArea(0, e.getX(), e.getY(), e.getA(), e.getB(),
					mapBuilder.getSelected());
		}
	}
	
	public static void main(String[] args) {
		new REDAD();
	}

	@Override
	public void ClientEventPerformed(ClientEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clientConnected(ClientEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clientDisconnected(ClientEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clientAuthenticated(ClientEvent e) {
		// TODO Auto-generated method stub
		
	}
}
